﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_DanhSachCanBoDoan : System.Web.UI.Page
{
    _dbDataContext _db = new _dbDataContext();
    static string donviauto = "";
    readonly SecurityLib.Sec_AES _se = new SecurityLib.Sec_AES();
    public string st = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        donviauto = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128);
        if (!IsPostBack)
        {
            st = _se.Decrypt(Request.QueryString["tid"].Replace(" ", "+"), "gold", 128);
            drlchuyenden.DataSource = PageURL.GetListCoQuan();
            drlchuyenden.DataTextField = "TenCoQuan";
            drlchuyenden.DataValueField = "ID";
            drlchuyenden.DataBind();
            BindGrid();
        }
    }
    protected void btn_timkiem_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void btnclose_Click(object sender, EventArgs e)
    {
        pntcontent.Visible = true;
        pnketnapdang.Visible = false;
    }
    protected void LinkButtonClose_Click(object sender, EventArgs e)
    {
        pntcontent.Visible = true;
        pnketnapdang.Visible = false;
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        if (rdb1.Checked == true)//truong hop chuyen ra ngoai he thong
        {
            if (txtNgayChuyenCongTac.Text.Length == 0)
            {
                lblthanhcong.Text = "Chọn ngày chuyển công tác trước khi thực hiện chuyển.";
                return;
            }
            if (txttencoquanchuyenden.Text.Length == 0)
            {
                lblthanhcong.Text = "nhập tên đơn vị chuyện đến trước khi thực hiện chuyển.";
                return;
            }
            //chen thong tin chuyen di vao table luanchuyencanbodoan
            tblLuanChuyenCanBoDoan _tb = new tblLuanChuyenCanBoDoan();
            _tb.DonViCuID = txtdonvicu.Value;
            _tb.ChucVuCuID = int.Parse(txtchucvucu.Value);
            _tb.DonViChuyenDenID = int.Parse(drlchuyenden.SelectedValue.ToString());
            _tb.TenDonViChuyenDen = txttencoquanchuyenden.Text;
            _tb.NgayChuyenCongTac = CatalogAccess.ConvertToDate(txtNgayChuyenCongTac.Text);
            _tb.LuanChuyenCanBo = false;
            _tb.MaDoanVien = txtmadoanvienchuyen.Value;
            try
            {
                _db.tblLuanChuyenCanBoDoans.InsertOnSubmit(_tb);
                _db.SubmitChanges();
                //xoa can bo doan nay ra khoi table congtacdoan
                var delmadoanvien = _db.tblCongTacDoans.Single(r => r.MaDoanVien.Trim() == txtmadoanvienchuyen.Value);
                try
                {
                    _db.tblCongTacDoans.DeleteOnSubmit(delmadoanvien);
                    _db.SubmitChanges();
                    //cap nhat trang thai doan vien nay ve =8 (8 la trang thai cua doan vien da chuyen sang khoi khac ngoai he thong)
                    CatalogAccess.CapNhatTrangThaiDoanVien(txtmadoanvienchuyen.Value, 8);
                    lblthanhcong.Text = "Luân chuển thành công.";
                    //load lai dữ liệu
                    string donvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128); //MemberShip_ASP.GetMaDonViFromUserName(Page.User.Identity.Name);
                    drlchuyenden.DataSource = PageURL.GetListCoQuan();
                    drlchuyenden.DataTextField = "TenCoQuan";
                    drlchuyenden.DataValueField = "ID";
                    drlchuyenden.DataBind();
                    var list = from p in _db.tblDoanViens
                               where p.TrucThuoc.Trim() == donvi
                               join c in _db.tblCongTacDoans on p.MaDonVien equals c.MaDoanVien
                               select new PageURL.CanBoDOan
                               {
                                   MaDoanVien = p.MaDonVien.Trim(),
                                   TenDoanVien = p.HoTen.Trim(),
                                   DonViCongTac = c.tblCoSoDoan.TenDonVi.Trim(),
                                   NgayBacDauCongTac = c.NgayCongTac,
                                   ChucVu = c.tblDanhMuc2.TenDanhMuc.Trim(),
                                   ChucVuCu = c.ChucVuHienTai,
                                   DonViCongTacCu = c.DonViTrucThuoc
                               };
                    BindGrid();
                }
                catch { }
            }
            catch { }
        }
        else//truong hop chuyen sang don vi khac trong cung he thong
        {
            if (txtNgayluanchuyencanbodoan.Text.Length == 0)
            {
                lblthanhcong.Text = "Chọn ngày chuyển công tác trước khi thực hiện chuyển.";
                return;
            }
            if (drldonviluanchuyenden.SelectedValue.ToString() == "-1")
            {
                lblthanhcong.Text = "Chọn đơn vị chuyển đến trước khi thực hiện chuyển.";
                return;
            }
            if (drlchucvunoichuyenden.SelectedValue.ToString() == "-1")
            {
                lblthanhcong.Text = "Chọn chức vụ mới trước khi thực hiện chuyển.";
                return;
            }
            tblLuanChuyenCanBoDoan _tb = new tblLuanChuyenCanBoDoan();
            _tb.DonViCuID = txtdonvicu.Value;
            _tb.ChucVuCuID = int.Parse(txtchucvucu.Value);
            _tb.NgayChuyenCongTac = CatalogAccess.ConvertToDate(txtNgayluanchuyencanbodoan.Text);
            _tb.LuanChuyenCanBo = true;
            _tb.DonViMoiID = drldonviluanchuyenden.SelectedValue.ToString();
            _tb.ChucVuMoiID = int.Parse(drlchucvunoichuyenden.SelectedValue.ToString());
            _tb.MaDoanVien = txtmadoanvienchuyen.Value;
            try
            {
                _db.tblLuanChuyenCanBoDoans.InsertOnSubmit(_tb);
                _db.SubmitChanges();
                //update lai thong tin can bo doan trong table congtacdoan
                var updatemadoanvien = _db.tblCongTacDoans.Single(r => r.MaDoanVien.Trim() == txtmadoanvienchuyen.Value);
                updatemadoanvien.NgayCongTac = CatalogAccess.ConvertToDate(txtNgayluanchuyencanbodoan.Text);
                updatemadoanvien.DonViTrucThuoc = drldonviluanchuyenden.SelectedValue.ToString();
                updatemadoanvien.ChucVuHienTai = int.Parse(drlchucvunoichuyenden.SelectedValue.ToString());
                try
                {
                    _db.SubmitChanges();
                    lblthanhcong.Text = "Luân chuển thành công.";
                    //load lai dữ liệu
                    string donvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128); //MemberShip_ASP.GetMaDonViFromUserName(Page.User.Identity.Name);
                    drlchuyenden.DataSource = PageURL.GetListCoQuan();
                    drlchuyenden.DataTextField = "TenCoQuan";
                    drlchuyenden.DataValueField = "ID";
                    drlchuyenden.DataBind();
                    var list = from p in _db.tblDoanViens
                               where p.TrucThuoc.Trim() == donvi
                               join c in _db.tblCongTacDoans on p.MaDonVien equals c.MaDoanVien
                               select new PageURL.CanBoDOan
                               {
                                   MaDoanVien = p.MaDonVien.Trim(),
                                   TenDoanVien = p.HoTen.Trim(),
                                   DonViCongTac = c.tblCoSoDoan.TenDonVi.Trim(),
                                   NgayBacDauCongTac = c.NgayCongTac,
                                   ChucVu = c.tblDanhMuc2.TenDanhMuc.Trim(),
                                   ChucVuCu = c.ChucVuHienTai,
                                   DonViCongTacCu = c.DonViTrucThuoc
                               };
                    BindGrid();
                }
                catch { }
            }
            catch { }
        }
    }
    //load du lieu vao gridview
    private void BindGrid()
    {
        string donvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128); //MemberShip_ASP.GetMaDonViFromUserName(Page.User.Identity.Name);
        if (txtHoten.Text.Length == 0)
        {
            var list = from p in _db.tblDoanViens
                       where p.TrucThuoc.Trim() == donvi
                       join c in _db.tblCongTacDoans on p.MaDonVien equals c.MaDoanVien
                       select new PageURL.CanBoDOan
                       {
                           MaDoanVien = p.MaDonVien.Trim(),
                           TenDoanVien = p.HoTen.Trim(),
                           DonViCongTac = c.tblCoSoDoan.TenDonVi.Trim(),
                           NgayBacDauCongTac = c.NgayCongTac,
                           ChucVu = c.tblDanhMuc2.TenDanhMuc.Trim(),
                           ChucVuCu = c.ChucVuHienTai,
                           DonViCongTacCu = c.DonViTrucThuoc
                       };
            Grid_DSCanBoDoan.DataSource = list.ToList();
            Grid_DSCanBoDoan.DataBind();
        }
        else
        {

            var list = from p in _db.tblDoanViens
                       where p.HoTen.Contains(txtHoten.Text) && p.TrucThuoc.Trim() == donvi
                       join c in _db.tblCongTacDoans on p.MaDonVien equals c.MaDoanVien
                       select new PageURL.CanBoDOan
                       {
                           MaDoanVien = p.MaDonVien.Trim(),
                           TenDoanVien = p.HoTen.Trim(),
                           DonViCongTac = c.tblCoSoDoan.TenDonVi.Trim(),
                           NgayBacDauCongTac = c.NgayCongTac,
                           ChucVu = c.tblDanhMuc2.TenDanhMuc.Trim(),
                           ChucVuCu = c.ChucVuHienTai,
                           DonViCongTacCu = c.DonViTrucThuoc
                       };
            Grid_DSCanBoDoan.DataSource = list.ToList();
            Grid_DSCanBoDoan.DataBind();
        }
    }
    //autocomplete by name
    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList_byname(string prefixText)
    {
        _dbDataContext _db = new _dbDataContext();
        var val = (from p in _db.tblDoanViens
                   where p.HoTen.Contains(prefixText) && p.TrucThuoc.Trim() == donviauto
                   join c in _db.tblCongTacDoans on p.MaDonVien equals c.MaDoanVien
                   select new PageURL.CanBoDOan
                   {
                       MaDoanVien = p.MaDonVien.Trim(),
                       TenDoanVien = p.HoTen.Trim(),
                       DonViCongTac = c.tblCoSoDoan.TenDonVi.Trim(),
                       NgayBacDauCongTac = c.NgayCongTac,
                       ChucVu = c.tblDanhMuc2.TenDanhMuc.Trim(),
                       ChucVuCu = c.ChucVuHienTai,
                       DonViCongTacCu = c.DonViTrucThuoc
                   }).ToList();

        string[] items = new string[val.ToList().Count];
        int i = 0;
        foreach (var item in val)
        {
            items.SetValue(item.TenDoanVien.Trim(), i);
            i++;
        }
        return items;
    }
    protected void rdb1_CheckedChanged(object sender, EventArgs e)
    {
        pnchuyensangcoquankhac.Visible = true;
        pnluanchuyencanbodoan.Visible = false;
        lblthanhcong.Text = "";
    }
    protected void rdb2_CheckedChanged(object sender, EventArgs e)
    {
        pnchuyensangcoquankhac.Visible = false;
        pnluanchuyencanbodoan.Visible = true;
        lblthanhcong.Text = "";
        BindDataToDrl();
    }
    private void BindDataToDrl()
    {
        drldonviluanchuyenden.Items.Clear();
        drlchucvunoichuyenden.Items.Clear();
        string madonvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128); //MemberShip_ASP.GetMaDonViFromUserName(this.Page.User.Identity.Name);
        ListItem item = new ListItem("---Chọn đơn vị---", "-1");
        drldonviluanchuyenden.Items.Add(item);
        var toplist = _db.tblCoSoDoans.Single(r => r.MaDonVi.Trim() == "TDB");
        item = new ListItem(toplist.TenDonVi.Trim(), toplist.MaDonVi.Trim());
        drldonviluanchuyenden.Items.Add(item);
        var firstlist = _db.tblCoSoDoans.Where(r => r.TrucThuoc.Trim() == "TDB");
        foreach (var itemfirst in firstlist)
        {
            item = new ListItem("\xA0\xA0\xA0\xA0\xA0\xA0" + itemfirst.TenDonVi.Trim(), itemfirst.MaDonVi.Trim());
            drldonviluanchuyenden.Items.Add(item);
            var secondlist = _db.tblCoSoDoans.Where(r => r.TrucThuoc.Trim() == itemfirst.MaDonVi.Trim());
            foreach (var itemsecond in secondlist)
            {
                item = new ListItem("\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0" + itemsecond.TenDonVi.Trim(), itemsecond.MaDonVi.Trim());
                drldonviluanchuyenden.Items.Add(item);
            }
        }
        CatalogAccess.BindItemToDropDown(drlchucvunoichuyenden, 9);
    }
    protected void txtNgayChuyenCongTac_TextChanged(object sender, EventArgs e)
    {
        lblthanhcong.Text = "";
    }
    protected void Grid_DSCanBoDoan_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        txtmadoanvienchuyen.Value = Grid_DSCanBoDoan.DataKeys[e.RowIndex].Value.ToString();
        txtchucvucu.Value = ((Label)Grid_DSCanBoDoan.Rows[e.RowIndex].FindControl("lblchucvucu")).Text;
        txtdonvicu.Value = ((Label)Grid_DSCanBoDoan.Rows[e.RowIndex].FindControl("lbldonvicongtaccu")).Text;
        pntcontent.Visible = false;
        pnketnapdang.Visible = true;
        lblthanhcong.Text = "";
    }
}